﻿using DevComponents.DotNetBar.SuperGrid;
using SDATA;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SABI
{
    public partial class frm0076 : Form
    {
        string sql = "";
        string _ObjectID = "";

        public frm0076()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            (Application.OpenForms["frmHome"] as frmHOME).CloseTab(this);
        }

        private void frm0076_Load(object sender, EventArgs e)
        {

        }

        private void frm0076_Shown(object sender, EventArgs e)
        {
            for (int i = 0; i < GridDB1.PrimaryGrid.Columns.Count;i++ )
                GridDB1.PrimaryGrid.Columns[i].FilterMatchType = DevComponents.DotNetBar.SuperGrid.FilterMatchType.Wildcards;
            loadDATA_List();
        }

        public void loadDATA_List() 
        {
            sql = "SELECT ObjectTypeID ,ObjectTypeID + ' - ' + ObjectTypeName as ObjectTypeName FROM tb0044 ORDER BY ObjectTypeName";
            DataTable dtT, dt_lst = FRM.GetDataTable(sql);
            dtT = dt_lst.Clone();
            dtT.Rows.Add("%", "%  - Tất cả");
            dtT.Merge(dt_lst);
            lstViewObjectTypeID.DataSource = dtT;
            lstViewObjectTypeID.SelectedIndex = 0;
        }

        public void loadDATA_Grid1()
        {
            sql = "SELECT * FROM vi0115 WHERE CHARINDEX('" + FRM.ID_Group + "',GroupShow)>0 AND DivisionID='" + FRM.DivisionID + "' ";
            if (!chkDisabled.Checked) { sql += "AND Disabled=0 "; }
            if (lstViewObjectTypeID.SelectedIndex > 0) {
                sql += "AND ObjectTypeID='" + lstViewObjectTypeID.SelectedValue.ToString() + "' "; 
            }
            sql +="ORDER BY ObjectID";
            GridDB1.PrimaryGrid.DataSource = FRM.GetDataTable(sql);
            GridDB1.Update();
            GridDB1.PrimaryGrid.Footer.Text = string.Format("Tổng số dòng: {0:#,0}", GridDB1.PrimaryGrid.Rows.Count);
        }

        private void lstViewObjectTypeID_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadDATA_Grid1();
        }

        private void chkDisabled_CheckedChanged(object sender, EventArgs e)
        {
            loadDATA_Grid1();
        }

        private void GridDB1_CellDoubleClick(object sender, GridCellDoubleClickEventArgs e)
        {
            if (GridDB1.PrimaryGrid.VisibleRowCount == 0) { return; }
            nEdit();
        }

        void nAdd()
        {
            Form frm = new frm0092();
            frm.ShowDialog();
        }
        void nEdit()
        {
            Form frm = new frm0092(false, false, _ObjectID);
            frm.ShowDialog();
        }
        void nDelete()
        {
            if (GridDB1.PrimaryGrid.GetSelectedRows().Count < 1)
            {
                SDLL.DLL02.Alert("Bạn phải chọn dòng cần xóa!");
                return;
            }
            if (!SDLL.DLL02.Confirm("Bạn có chắc muốn xóa?", "Xác nhận")) { return; }


            sql = string.Format("Select top 1 1  From tb0191  Where ObjectID ='{0}'",_ObjectID);
            DataTable dtT1 = FRM.GetDataTable(sql);
            sql = string.Format("Select top 1 1  From tb3523  Where ObjectID ='{0}'", _ObjectID);
            DataTable dtT2 = FRM.GetDataTable(sql);
            sql = string.Format("Select top 1 1  From tb3531  Where ObjectID ='{0}'", _ObjectID);
            DataTable dtT3 = FRM.GetDataTable(sql);
            sql = string.Format("Select top 1 1  From tb3537  Where ObjectID ='{0}'", _ObjectID);
            DataTable dtT4 = FRM.GetDataTable(sql);
            if(dtT1.Rows.Count>0||dtT2.Rows.Count>0||dtT3.Rows.Count>0||dtT4.Rows.Count>0)
            {
                SDLL.DLL02.Alert("Đối tượng bị ràng buộc, không thể xóa!");
                return;
            }

            sql = "DELETE tb0045 WHERE ObjectID='{0}'";
            sql = string.Format(sql, _ObjectID);
            if (DATA.ExecuteNonQuery(sql, FRM.ConnectString))
            {
                loadDATA_Grid1();
            }
            else
            {
                SDLL.DLL02.Alert("Xóa dữ liệu không thành công");
            }
        }
        void nView()
        {
            Form frm = new frm0092(false, true, _ObjectID);
            frm.ShowDialog();
        }
        void nInfo()
        {
            string tn = "tb0045";
            string ws = string.Format("WHERE ObjectID='{0}'", _ObjectID);
            Form frm = new frm0067(tn, ws);
            frm.ShowDialog();
        }

        void nRefresh()
        {
            loadDATA_List();
            lstViewObjectTypeID.SelectedIndex = 0;
        }

        private void GridDB1_SelectionChanged(object sender, GridEventArgs e)
        {
            GridRow gr = (GridRow)e.GridPanel.ActiveRow;
            _ObjectID = gr.Cells["clObjectID"].Value.ToString();

        }

        private void mAdd_Click(object sender, EventArgs e)
        {
            nAdd();
        }

        private void mEdit_Click(object sender, EventArgs e)
        {
            nEdit();
        }

        private void mDelete_Click(object sender, EventArgs e)
        {
            nDelete();
        }

        private void mView_Click(object sender, EventArgs e)
        {
            nView();
        }

        private void mRefresh_Click(object sender, EventArgs e)
        {
            nRefresh();
        }

        private void mSystemInfo_Click(object sender, EventArgs e)
        {
            nInfo();
        }
    }
}
